package com.ruoyi.project.video.controller;

import com.ruoyi.project.video.domain.Video;
import com.ruoyi.project.video.service.IVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * 描述: VideoUrlController
 * 创建时间: 2023/2/22 17:36
 * 作者: guozy
 * 版本: 1.0
 */
@Controller
public class VideoUrlController {

    @Autowired
    private IVideoService videoService;

    @RequestMapping("/gkd")
    public String getVideoUrl(Model model) {
        int page = 1;
        int pageSize = 20;
        Video video = new Video();
        video.setType("60");
        List<Video> videoList = videoService.selectVideoList(video);
        videoList = videoList.stream().skip((page - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        String url = String.format("/%s/%s", pageSize, page + 1);
        model.addAttribute("url", url);
        model.addAttribute("videoList", videoList);
        return "showVideo";
    }

    @RequestMapping("/gkd/{type}/{pageSize}/{page}")
    public String getVideoUrl(Model model, @PathVariable(required = false) Integer pageSize, @PathVariable(required = false) Integer page, @PathVariable(required = false) String type) throws InterruptedException, ExecutionException {
        Video video = new Video();
        video.setType(type);
        List<Video> videoList = videoService.selectVideoList(video);
        videoList = videoList.stream().skip((page - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        String url = String.format("/%s/%s", pageSize, page + 1);
        if (videoList.size() != 0) {
            model.addAttribute("url", url);
            model.addAttribute("videoList", videoList);
        } else {
            getVideoUrl(model);
        }
        return "showVideo";
    }
}
